//
//  MPAddPlatformTableCell.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/7.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPAddPlatformTableCell.h"
#import "UIImage+Common.h"
#import "MPPlatformObject.h"
#import "MPCellButton.h"

@interface MPAddPlatformTableCell ()
@property (nonatomic, strong)UILabel* titleLabel;
@property (nonatomic, strong)UIActivityIndicatorView* loadingView;

@property (nonatomic, strong)UIImageView* topSeperatorView;
@property (nonatomic, strong)UIImageView* bottomSeperatorView;
@property (nonatomic, strong)UIImageView* bgImageView;

@end

@implementation MPAddPlatformTableCell

+ (CGFloat)rowHeightForObject:(id)object
{
    return 60.0f;
}

+ (CGFloat)estimateRowsHeight
{
    return 60.0f;
}

+ (BOOL)shouldUseAutoLayout
{
    return NO;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        [self.contentView addSubview:self.topSeperatorView];
        [self.contentView addSubview:self.bgImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.loadingView];
        [self.contentView addSubview:self.subscribeBtn];
        [self.contentView addSubview:self.bottomSeperatorView];
        
        [self setupConstraints];
        
        [self.bgImageView setImage:[UIImage imageFromColor:[UIColor whiteColor] imageSize:self.bgImageView.frame.size]];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    [self.subscribeBtn removeTarget:nil
                       action:NULL
             forControlEvents:UIControlEventAllEvents];
}

- (void)setupConstraints
{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.trailing.mas_equalTo(self);
    }];
    
    [self.topSeperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).with.offset(10.0f);
    }];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topSeperatorView.mas_bottom);
        make.leading.trailing.mas_equalTo(self.contentView);
    }];
    
    [self.bottomSeperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgImageView.mas_bottom);
        make.leading.trailing.bottom.mas_equalTo(self.contentView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView).with.offset(5.0f);
        make.leading.mas_equalTo(self.contentView).with.offset(10.0f);
        make.width.mas_equalTo(@(70.0f));
    }];
    
    [self.subscribeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView).with.offset(5.0f);
        make.trailing.mas_equalTo(self.contentView).with.offset(-10.0f);
//        make.leading.mas_equalTo(self.titleLabel.mas_trailing).with.offset(10.0f).with.priorityMedium();
    }];
    
    [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.subscribeBtn);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setObject:(id)object
{
    [super setObject:object];
    
    if (object == nil)
    {
        return ;
    }
    else
    {
        if ([object isKindOfClass:[MPPlatformObject class]]) {
            MPPlatformObject* platform = (MPPlatformObject *)object;
            if (![NSString stringIsEmpty:platform.platformName]) {
                self.titleLabel.text = platform.platformName;
            }
            
            if (platform.subStatus == eMPPlatformSubscribeStatusUnSubs) {
                [self.subscribeBtn setImage:[UIImage imageForKey:@"subscribe_platform_add_default"] forState:UIControlStateNormal];
                [self.subscribeBtn setImage:[UIImage imageForKey:@"subscribe_platform_add_focused"] forState:UIControlStateHighlighted];
            }
            else
            {
                [self.subscribeBtn setImage:[UIImage imageForKey:@"subscribe_platform_delete_default"] forState:UIControlStateNormal];
                [self.subscribeBtn setImage:[UIImage imageForKey:@"subscribe_platform_delete_focused"] forState:UIControlStateHighlighted];
            }
        }
    }
}

#pragma mark - property
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor colorWithHexString:kContentTextColor];
        _titleLabel.font = [UIFont systemFontOfSize:kContentFontSize];
    }
    return _titleLabel;
}

- (UIActivityIndicatorView *)loadingView
{
    if (!_loadingView) {
        _loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _loadingView.hidesWhenStopped = YES;
    }
    return _loadingView;
}

- (UIImageView *)topSeperatorView
{
    if (!_topSeperatorView) {
        _topSeperatorView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor] imageSize:CGSizeMake(SCREEN_WIDTH, 1.0f)]];
    }
    return _topSeperatorView;
}

- (UIImageView *)bottomSeperatorView
{
    if (!_bottomSeperatorView) {
        _bottomSeperatorView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor] imageSize:CGSizeMake(SCREEN_WIDTH, 1.0f)]];
    }
    return _bottomSeperatorView;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
    }
    return _bgImageView;
}

- (MPCellButton *)subscribeBtn
{
    if (!_subscribeBtn) {
        _subscribeBtn = [MPCellButton buttonWithType:UIButtonTypeCustom];
    }
    return _subscribeBtn;
}

#pragma mark - KVO

- (void)objectPropertyChanged:(NSString *)property {
    if([property isEqualToString:@"subStatus"]){
        if ([self.object isKindOfClass:[MPPlatformObject class]]) {
            MPPlatformObject* platform = (MPPlatformObject *)self.object;
            switch (platform.subStatus) {
                case eMPPlatformSubscribeStatusLoading:
                {
                    self.subscribeBtn.hidden = YES;
                    [self.loadingView startAnimating];
                }
                    break;
                case eMPPlatformSubscribeStatusSubs:
                {
                    [self.loadingView stopAnimating];
                    self.subscribeBtn.hidden = NO;
                    [self.subscribeBtn setImage:[UIImage imageForKey:@"subscribe_platform_delete_default"] forState:UIControlStateNormal];
                    [self.subscribeBtn setImage:[UIImage imageForKey:@"subscribe_platform_delete_focused"] forState:UIControlStateHighlighted];

                }
                    break;
                case eMPPlatformSubscribeStatusUnSubs:
                {
                    [self.loadingView stopAnimating];
                    self.subscribeBtn.hidden = NO;
                    [self.subscribeBtn setImage:[UIImage imageForKey:@"subscribe_platform_add_default"] forState:UIControlStateNormal];
                    [self.subscribeBtn setImage:[UIImage imageForKey:@"subscribe_platform_add_focused"] forState:UIControlStateHighlighted];
                }
                    break;
            }
        }
    }
}

- (void)startObserveObjectProperty {
    [self addObservedProperty:@"subStatus"];
}

- (void)finishObserveObjectProperty {
    [self removeObservedProperty:@"subStatus"];
}

@end
